require(['grid', 'topBar', 'date', 'select2', 'highcharts'], function () {
	//初始化顶部搜索栏
	function topBarInit() {
        $("#top").topBar({
            left: [
                {
                    type: 'filter-more',
                    dateType:'custom',
                    search: function () {
                        search();
                    },
                    item: [
                        {
                            text: '时间快捷',
                            name1: 'beginTime',
                            name2: 'endTime',
                            type: 'dateLine'
                        }
                    ]
                }
            ]
//        	,
//            right: [
//                {
//				    text: '查询',
//				    type: 'btn',
//				    authority: '',
//				    click: function () {
//				    	search();
//				    }
//				}
//            ]
        });
    }
	
	function search(){
		var postData = $("#top").parent().getValues();
		if(postData.beginTime && postData.endTime){
			//初始化报表
			$.ajaxExt({
				url : "/reportOperatDate/getLogColltTable.htm",
				data: $("#top").parent().getValues()
			}).done(function(data){
				if(data && data.length>0){
					$("#visitCount").html(data[0].visitCount+"<em>次</em>");
					$("#visitUserCount").html(data[0].visitUserCount+"<em>人</em>");
					$("#sVisitCount").html(data[0].sVisitCount+"<em>次</em>");
					$("#sVisitUserCount").html(data[0].sVisitUserCount+"<em>人</em>");
					$("#visitPer").html(data[0].visitPer);
				}
			});
			//初始化图1
			$.ajaxExt({
				url : "/reportOperatDate/getUsersAccessCollection.htm",
				data: $("#top").parent().getValues()
			}).done(function(data){
				var xdata = [];
				var series = [{name:data[0].CODE_DSC,data:[]}];
				var tmp_code = data[0].CODE_VALUE;
				$.each(data,function(index, item){
					if(tmp_code == item.CODE_VALUE){
						xdata.push(item.CORP_SHRTNM);
					}
				});
				var count = 0;
				$.each(data,function(index, item){
					if(tmp_code == item.CODE_VALUE){
						series[count].data.push(item.VISITS);
					}else{
						tmp_code = item.CODE_VALUE;
						series.push({name:item.CODE_DSC,data:[]});
						count++;
						series[count].data.push(item.VISITS);
					}
				});
				initMultiBargraph($("#usersAccessCollection"),"各客户访问情况一览（次）",xdata,series,-45);
			});
			//初始化图2
			$.ajaxExt({
				url : "/reportOperatDate/getDistributionByFunc.htm",
				data: $("#top").parent().getValues()
			}).done(function(result){
				var series =[{
					type: 'pie',
					name: '次数占百分比',
					data: []
				}];
				$.each(result,function(index,item){
					var ar = [];
					ar.push(item.CODE_DSC);
					if(item.VISITS)
						ar.push(item.VISITS);
					else
						ar.push(0);
					series[0].data.push(ar);
				});
				initPieGraph($("#distributionByFunc"),"平台点击类型分布",series);
			});
			//初始化图3
			$.ajaxExt({
				url : "/reportOperatDate/getUsersAccessRateByDays.htm",
				data: $("#top").parent().getValues()
			}).done(function(result){
				var series = [];
				var xdata = [];
				//新建数组
				var tmp_Date = result[0].DAYS;
				xdata.push(tmp_Date);
				$.each(result,function(index,item){
					if(item.DAYS==tmp_Date){
						series.push({
							name:item.CORP_SHRTNM,
							data:[]
						});
					}
				});
				var count=0;
				$.each(result,function(index,item){
					if(tmp_Date==item.DAYS){
						series[count].data.push(item.VISITS);
						count++;
					}else{
						xdata.push(item.DAYS);
						series[0].data.push(item.VISITS);
						count = 1;
						tmp_Date = item.DAYS;
					}
				});

				initCurveGraph($("#usersAccessRateByDays"), "时间段内客户访问频率（次）", xdata, series);
			});
			//初始化图4
			$.ajaxExt({
				url : "/reportOperatDate/getUsersAccessRateByHours.htm",
				data: $("#top").parent().getValues()
			}).done(function(data){
				var cnt = 0;
				if(data){
					cnt = data.length/24;
				}
				var series = [];
				var xdata = [];
				var tmp_hour = data[0].DAYS;
				$.each(data,function(index,item){
					if(index < cnt){
						series.push({name:item.CORP_SHRTNM,data:[]});
					}
				});
				xdata.push(data[0].DAYS);
				var count = 0;
				$.each(data,function(index,item){
					if(item.DAYS == tmp_hour){
						series[count].data.push(item.VISITS);
						count++;
					}else{
						xdata.push(item.DAYS+"h");
						series[0].data.push(item.VISITS);
						count = 1;
						tmp_hour = item.DAYS;
					}
				});
				initMultiBargraph($("#usersAccessRateByHours"),"各时间点客户使用频率一览（次）",xdata,series,0);
			});
			//初始化图5
			$.ajaxExt({
				url : "/reportOperatDate/getTop10OfQuery.htm",
				data: $("#top").parent().getValues()
			}).done(function(data){
				var series = [{name:'时长', data:[]}];
				var xdata = [];
				$.each(data,function(index,item){
					xdata.push(item.FUNC_NAME);
					series[0].data.push(item.MNPLT_DURA);
				});
				initColumnGraph($("#top10OfQuery"), "前十大耗时查询排名（秒）", xdata, series);
			});
			//初始化图6
			$.ajaxExt({
				url : "/reportOperatDate/getTop10OfFunc.htm",
				data: $("#top").parent().getValues()
			}).done(function(data){
				var xdata = [];
				var series = [{name:'次数', data:[]}];
				$.each(data,function(index,item){
					xdata.push(item.FUNC_NAME);
					series[0].data.push(item.VISIT_COUNT);
				});
				initBarGraph($("#top10OfFunc"), "前十大访问功能排名（次）", xdata, series);
			});
		}else{
			$.messager.alert("提示","请选择时间。","info");
		}
	}
	
	//多维柱状图
	function initMultiBargraph(obj, text, xdata, series, rotation){
		obj.highcharts({
			chart: {
				type: 'column'
			},
			title: {
				text: text
			},
			xAxis: {
				categories:xdata,
				labels: {
					rotation : rotation
				}
			},
			yAxis: {
				min: 0,
				title: {
					text: ''
				},
				stackLabels: {
					enabled: true,
					style: {
						fontWeight: 'bold',
						color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
					}
				}
			},
			legend: {
				verticalAlign: 'bottom',
				floating: false,
				backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
				borderColor: '#CCC',
				borderWidth: 1,
				shadow: false
			},
			tooltip: {
				formatter: function() {
					return '<b>'+ this.x +'</b><br>'+
							this.series.name +': '+ this.y +"次" +'<br>'+
							'Total: '+ this.point.stackTotal +"次";
				}
			},
			plotOptions: {
				column: {
					stacking: 'normal',
					dataLabels: {
						enabled: false,
						color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
					}
				}
			},
			series: series,
			credits: {
				enabled: false
			}
		});
	}
	
	//饼图
	function initPieGraph(obj, text, series){
		obj.highcharts({
			chart: {
				plotBackgroundColor: null,
				plotBorderWidth: null,
				plotShadow: false
			},
			title: {
				text: text
			},
			tooltip: {
				pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b><br>次数: <b>{point.y} 次 </b>'
			},
			plotOptions: {
				pie: {
					allowPointSelect: true,
					cursor: 'pointer',
					dataLabels: {
						enabled: false,
						//color: '#000000',
						//connectorColor: '#000000',
						//format: '<b>{point.name}</b>: {point.y} 次 '
					},
					showInLegend: true
				}
			},
			legend: {
				verticalAlign: 'bottom',
				labelFormatter: function() {
					return this.name;
				},
				floating: false
			},
			series: series,
			credits: {
				enabled: false
			}
		});
	}
	
	//曲线图
	function initCurveGraph(obj, text, xdata, series){
		obj.highcharts({
			title: {
				text: text,
				x: -20
			},
			xAxis: {
				categories: xdata,
				labels: {
					rotation : -45
				}
			},
			yAxis: {
				title: {
					text:''
				},
				plotLines: [{
					value: 0,
					width: 1,
					color: '#808080'
				}]
			},
			tooltip: {
				valueSuffix: '',
				pointFormat: '{point.y} 次'
			},
			legend: {
				verticalAlign: 'bottom',
				floating: false
			},
			series: series,
			credits: {
				enabled: false
			}
		});
	}
	
	//柱状图
	function initColumnGraph(obj, text, xdata, series){
		obj.highcharts({
			chart: {
				type: 'column'
			},
			title: {
				text: text,
				x: -20
			},
			credits: {
				enabled: false
			},
			xAxis: {
				categories: xdata,
				labels: {
					rotation : -45
				}
			},
			yAxis: {
				title: {
					text: ''
				}
			},
			tooltip: {
				pointFormat: '{point.y} 秒'
			},
			legend: {
				layout: 'vertical',
				align: 'right',
				verticalAlign: 'middle',
				borderWidth: 0
			},
			series: series
		});
	}
	
	//条形图
	function initBarGraph(obj, text, xdata, series){
		obj.highcharts({
			chart: {
				type: 'bar'
			},
			title: {
				text: text
			},
			credits: {
				enabled: false
			},
			xAxis: {
				categories: xdata
			},
			yAxis: {
				lineWidth: 1,
				title: {
					text: ''
				}
			},
			tooltip: {
				pointFormat: '{point.y} 次'
			},
			legend: {
				layout: 'vertical',
				align: 'right',
				verticalAlign: 'middle',
				borderWidth: 0
			},
			series: series
		});
	}
	
	topBarInit();
});